package extend.client.httpclient;

import extend.client.base.*;
import hehe.log.collect.SyncCollect;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * httpclient 传输类
 * <B>说明：</B>
 * <pre>
 *  略
 * </pre>
 * <B>示例：</B>
 * <pre>
 *  略
 * </pre>
 * <B>日志：</B>
 * <pre>
 *  略
 * </pre>
 * <B>注意事项：</B>
 * <pre>
 *  略
 * </pre>
 */
public class HttpClient extends Transport
{
    public Request send(Request request)
    {
        // 首次启动,这里会比较耗时,可能这里开启线程了,后面效率提高
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpRequestBase httpRequest = this.buildHttpClientRequest(request);
        try {
            HttpResponse httpResponse = httpclient.execute(httpRequest);
            HttpClientResponse httpClientResponse = (HttpClientResponse)request.getRequestParams().createResponse();
            httpClientResponse.setResponse(httpResponse);
            request.setResponse(httpClientResponse);

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return request;
    }

    /**
     * 构建一个httpclient HttpRequest 请求
     *<B>说明：</B>
     *<pre>
     * 略
     *</pre>
     */
    public HttpRequestBase buildHttpClientRequest(Request request)
    {

        RequestParams requestParams = request.getRequestParams();
        String method = requestParams.getMethod();
        String url = requestParams.buildUrl(request.getUri());
        HttpRequestBase httpRequestBase = null;
        switch (method) {
            case Method.GET_METHOD :
                httpRequestBase = this.buildHttpClientGetRequest(request,new HttpGet(url));
                break;
            case Method.POST_METHOD :
                httpRequestBase = this.buildHttpClientPostRequest(request);
                break;
        }

        return httpRequestBase;
    }

    public void setHeader(Request request,HttpRequestBase httpRequestBase)
    {
        if (request.hasHeader()) {
            Iterator<Map.Entry<String, String>> it = request.getHeader().getIterator();
            while (it.hasNext()) {
                Map.Entry<String, String> entry = it.next();
                httpRequestBase.setHeader(entry.getKey(),entry.getValue());
            }
        }
    }

    public HttpRequestBase buildHttpClientGetRequest(Request request,HttpRequestBase httpRequestBase)
    {
        request.prepare();
        this.setHeader(request,httpRequestBase);
        RequestConfig.Builder builder = RequestConfig.custom();
        httpRequestBase.setConfig(builder.build());

        return httpRequestBase;
    }

    public HttpRequestBase buildHttpClientPostRequest(Request request)
    {
        request.prepare();
        RequestParams requestParams = request.getRequestParams();
        String url = requestParams.buildUrl(request.getUri());
        HttpPost httpPost = new HttpPost(url);
        this.setHeader(request,httpPost);

        httpPost.setEntity(new StringEntity(request.getContent(),"UTF-8"));
        RequestConfig.Builder builder = RequestConfig.custom();
        httpPost.setConfig(builder.build());

        return httpPost;
    }
}
